import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
  ActivatedRoute
 } from '@angular/router';
import {
  pluck,
  map,
  switchMap,
} from 'rxjs/operators';

const BaseUrl = '/api';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor(
    private http: HttpClient,
    private activatedRoute: ActivatedRoute
  ) { }

  getMenuList() {
      return this.http.get(`${BaseUrl}/menu`);
  }

  getCoffees() {
    const getData = (id: string) => this.http.get(`${BaseUrl}/coffees`, {
      params: {id}
    }).pipe(pluck('0'));

    return this.activatedRoute.queryParams.pipe(
      pluck('id'),
      switchMap(getData)
    );
  }

}
